#import "CityCRViewController.h"
#import "ProvinceAndCity.h"
#import "LocationViewController.h"
@implementation CityCRViewController
@synthesize citys;

-(id) initWithCoder:(NSCoder *)aDecoder{
	if (self = [super initWithCoder: aDecoder]) {
		firstTime = YES;
	}
	return self;
}

-(void) viewDidLoad{
	UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle: @"新增" style: UIBarButtonItemStyleDone target:self action:@selector(goCitySelectView)];
	[self.navigationItem setRightBarButtonItem: item];
	[item release];
	self.navigationItem.title = @"关注城市";
	
	if (firstTime == YES) {
		firstTime = NO;
		NSString *lastCheck = [[NSUserDefaults standardUserDefaults] stringForKey: kLastCheck];
		if (lastCheck) {
			NSArray *valuePart = [lastCheck componentsSeparatedByString: @"_"];
			WeatherResponse *response = [[WeatherResponse alloc] initWithListener: viewController provinceName:[valuePart objectAtIndex: 0] cityName:[valuePart objectAtIndex: 1]];
			viewController.response = response;
			[response load];
			[response release];
			[self.navigationController pushViewController: viewController animated:YES];
		}
	}
}


-(void) viewWillAppear:(BOOL)animated{
	citys = [[NSMutableArray alloc] initWithArray: [[NSUserDefaults standardUserDefaults] arrayForKey: kPreferCityArray]];
	[_tableView reloadData];
}

-(void)goCitySelectView{
	LocationViewController *control = [[LocationViewController alloc] initWithNibName: @"LocationViewController" bundle: [NSBundle mainBundle]];
	[self.navigationController pushViewController: control animated:YES];	
	[control release];
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
	return [citys count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *CITY_ROW_ID = @"CITY_ROW_ID";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CITY_ROW_ID];
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CITY_ROW_ID];
	}
	cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	cell.textLabel.text = [citys objectAtIndex: [indexPath row]];
	return cell;
}


-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
	NSString *value = [citys objectAtIndex: [indexPath row]];
	NSArray *valuePart = [value componentsSeparatedByString: @"_"];
	WeatherResponse *response = [[WeatherResponse alloc] initWithListener: viewController provinceName:[valuePart objectAtIndex: 0] cityName:[valuePart objectAtIndex: 1]];
	viewController.response = response;
	[response load];
	[response release];
	[self.navigationController pushViewController: viewController animated:YES];
}

-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
	return YES;
}

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
	if (editingStyle ==  UITableViewCellEditingStyleDelete) {
		[citys removeObjectAtIndex: [indexPath row]];
		[[NSUserDefaults standardUserDefaults] setValue:citys forKey: kPreferCityArray];
		[_tableView reloadData];
	}
}

- (void)dealloc {
	[citys removeAllObjects];
	[citys release];
	[_tableView release];
	[viewController release];
    [super dealloc];
}

@end
